ENT.Type = "brush"

local prohibitedEntities = { func_brush = true }

function ENT:Initialize( )
	local x,y = self:WorldSpaceAABB()
	self.center = self:OBBCenter()
	self.normal = self.center:Normalize()
	self:SetTrigger( true )
	self:SetNotSolid( true )
	self.Enabled = true
end

cells.ContraptionsInTransition = {}
cells.ContraptionID = 1

function ENT:StartTouch( ent )
	if prohibitedEntities[ ent:GetClass() ] then return end
	if ent.ContraptionID != nil then return end
	if ent.CellExit then return end
	if ent:IsPlayer() and ent:InVehicle() then return end

	local entities = cells.GetConstrainedEntsWithSelf( ent )

	cells.ContraptionsInTransition[ cells.ContraptionID ] = {}
	MsgN("\nCreated new contraption for transition, ID: " .. cells.ContraptionID)

	for k,v in pairs( entities ) do
		v.ContraptionID = cells.ContraptionID
		v.InTransition = true
		cells.ContraptionsInTransition[ cells.ContraptionID ][ v:EntIndex() ] = v
		print("	" .. v:EntIndex() .. " | " .. v:GetClass())
	end

	cells.ContraptionID = cells.ContraptionID + 1
end

function ENT:EndTouch( ent )
	if prohibitedEntities[ ent:GetClass() ] then return end
	if ent:IsPlayer() and ent:InVehicle() then return end
	if !ent.ContraptionID then MsgN("Warning! " .. ent:GetClass() .. " [" .. ent:EntIndex() .. "] is exiting a cell link without a contraption ID!") return end
	if ent.CellExit then return end
	ent.InTransition = nil

	local conID = ent.ContraptionID

	for k,v in pairs( cells.ContraptionsInTransition[ conID ] ) do
		if !v:IsValid() or v.InTransition then return end

		local velocity = 0
		local phys = v:GetPhysicsObject()

		if phys:IsValid() then velocity = phys:GetVelocity() else velocity = v:GetVelocity() end
		v:SetPos( v:GetPos() )
		if phys:IsValid() then phys:SetVelocity( velocity ) else v:SetVelocity( velocity ) end

		v.CellExit = self.exit
		v.ContraptionID = nil

		timer.Simple( 0.3, function(v) v.CellExit = nil end, v)

	end

	cells.ContraptionsInTransition[ conID ] = nil
end

--[[function ENT:Touch( ent )
	if !prohibitedEntities[ent:GetClass()] and !(ent.CellLinkActive and ent.CellExit == self) and !(ent:IsPlayer() and ent:InVehicle()) and !ent:IsPlayerHolding() and self.Enabled then	
		local _ents = cells.GetConstrainedEntsWithSelf( ent )	
			
		for _, v in ipairs( _ents ) do
			v.CellLinkActive = true
			v.CellExit = self.exit
			
			local phys = ent:GetPhysicsObject()
			local velocity
			if phys:IsValid() then velocity = phys:GetVelocity() else velocity = ent:GetVelocity() end
			
			v:SetPos( v:GetPos() + self.normal )

			if phys:IsValid() then phys:SetVelocity( velocity ) else ent:SetVelocity( velocity ) end
		end
		
		timer.Simple( 0.3, function( _ents ) for _, v in ipairs( _ents ) do if IsValid(v) then v.CellLinkActive = false v.CellExit = nil end end end, _ents )
	end
end]]

function ENT:Think( )
	if !self.exit then
		for k,v in pairs( ents.FindByClass("func_cell_link" ) ) do
			if v.center == -self.center then
				self.exit = v
				v.exit = self
				return
			end
		end
	end
end